package com.hudongwx.food.activity;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.fargment.HomepageFragment;
import com.hudongwx.food.login_fargment.ShoujiFragment;
import com.hudongwx.food.login_fargment.YunmaFragment;

public class LoginActivity extends AppCompatActivity {
    private TabLayout tableLayout;
    FragmentManager manager;
    private FragmentTransaction transaction;
    private ViewPager viewPager;
    private ShoujiFragment shoujiFragment;
    private YunmaFragment yunmaFragment;
    private TextView register;
    private ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tableLayout = (TabLayout) findViewById(R.id.login_tabLayout);
        viewPager = (ViewPager) findViewById(R.id.login_viewPager);
        register = (TextView) findViewById(R.id.login_register);
        img = (ImageView) findViewById(R.id.login_img);
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();
        viewPager.setAdapter(new Myadapter(manager));
        tableLayout.setupWithViewPager(viewPager);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegisterActivity.class);
                LoginActivity.this.startActivity(intent);
            }
        });
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
    class Myadapter extends FragmentStatePagerAdapter{

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (null==shoujiFragment){
                shoujiFragment = new ShoujiFragment();
            }if (null==yunmaFragment){
                yunmaFragment = new YunmaFragment();
            }
            switch (position){
                case 0:
                    return shoujiFragment;
                case 1:
                    return yunmaFragment;
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "手机号登陆";
                case 1:
                    return "云码登陆";

            }
            return null;
        }
    }
}
